#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wav_codec.h"

/* 读取文件的辅助函数 */
static unsigned char* read_file(const char* filename, unsigned int* size) {
    FILE* file;
    unsigned char* buffer;
    long file_size;

    file = fopen(filename, "rb");
    if (file == NULL) {
        printf("无法打开文件: %s\n", filename);
        return NULL;
    }

    /* 获取文件大小 */
    fseek(file, 0, SEEK_END);
    file_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    /* 分配缓冲区 */
    buffer = (unsigned char*)malloc(file_size);
    if (buffer == NULL) {
        printf("内存分配失败\n");
        fclose(file);
        return NULL;
    }

    /* 读取文件内容 */
    if (fread(buffer, 1, file_size, file) != (size_t)file_size) {
        printf("读取文件失败\n");
        free(buffer);
        fclose(file);
        return NULL;
    }

    fclose(file);
    *size = (unsigned int)file_size;
    return buffer;
}

/* 写入文件的辅助函数 */
static int write_file(const char* filename, const unsigned char* data, unsigned int size) {
    FILE* file;
    
    file = fopen(filename, "wb");
    if (file == NULL) {
        printf("无法创建文件: %s\n", filename);
        return -1;
    }
    
    if (fwrite(data, 1, size, file) != size) {
        printf("写入文件失败\n");
        fclose(file);
        return -1;
    }
    
    fclose(file);
    return 0;
}

/* 压缩WAV文件的函数 */
static int compress_wav_file(const char* input_file, const char* output_file) {
    unsigned char* wav_data = NULL;
    unsigned int wav_size = 0;
    unsigned char* compressed_data = NULL;
    unsigned int compressed_size = 0;
    CodecConfig config;
    int result;
    
    /* 读取WAV文件 */
    wav_data = read_file(input_file, &wav_size);
    if (wav_data == NULL) {
        return 1;
    }
    
    printf("原始WAV文件大小: %u 字节\n", wav_size);
    
    /* 初始化压缩配置 */
    codec_init_config(&config);
    
    /* 分配压缩数据缓冲区 - 假设压缩后大小不会超过原始大小 */
    compressed_data = (unsigned char*)malloc(wav_size);
    if (compressed_data == NULL) {
        printf("内存分配失败\n");
        free(wav_data);
        return 1;
    }
    
    /* 压缩WAV数据 */
    compressed_size = wav_size;
    result = wav_compress(wav_data, wav_size, compressed_data, &compressed_size, &config);
    
    if (result != WAV_OK) {
        printf("压缩失败，错误码: %d\n", result);
        free(wav_data);
        free(compressed_data);
        return 1;
    }
    
    printf("压缩后大小: %u 字节 (%.2f%%)\n", 
           compressed_size, (float)compressed_size * 100.0f / wav_size);
    
    /* 保存压缩数据 */
    if (write_file(output_file, compressed_data, compressed_size) != 0) {
        free(wav_data);
        free(compressed_data);
        return 1;
    }
    
    /* 释放资源 */
    free(wav_data);
    free(compressed_data);
    
    printf("压缩完成！\n");
    return 0;
}

/* 解压缩WAV文件的函数 */
static int decompress_wav_file(const char* input_file, const char* output_file) {
    unsigned char* compressed_data = NULL;
    unsigned int compressed_size = 0;
    unsigned char* decompressed_data = NULL;
    unsigned int decompressed_size = 0;
    int result;
    
    /* 读取压缩文件 */
    compressed_data = read_file(input_file, &compressed_size);
    if (compressed_data == NULL) {
        return 1;
    }
    
    printf("压缩文件大小: %u 字节\n", compressed_size);
    
    /* 分配解压数据缓冲区 - 假设解压后大小是压缩大小的4倍（足够大） */
    decompressed_size = compressed_size * 4;
    decompressed_data = (unsigned char*)malloc(decompressed_size);
    if (decompressed_data == NULL) {
        printf("内存分配失败\n");
        free(compressed_data);
        return 1;
    }
    
    /* 解压缩WAV数据 */
    result = wav_decompress(compressed_data, compressed_size, 
                          decompressed_data, &decompressed_size);
    
    if (result != WAV_OK) {
        printf("解压缩失败，错误码: %d\n", result);
        free(compressed_data);
        free(decompressed_data);
        return 1;
    }
    
    printf("解压后大小: %u 字节\n", decompressed_size);
    
    /* 保存解压数据 */
    if (write_file(output_file, decompressed_data, decompressed_size) != 0) {
        free(compressed_data);
        free(decompressed_data);
        return 1;
    }
    
    /* 释放资源 */
    free(compressed_data);
    free(decompressed_data);
    
    printf("解压缩完成！\n");
    return 0;
}

/* 显示帮助信息 */
static void show_usage(const char* program_name) {
    printf("用法:\n");
    printf("  压缩:   %s -c <输入WAV文件> <压缩输出文件>\n", program_name);
    printf("  解压缩: %s -d <输入压缩文件> <解压输出WAV文件>\n", program_name);
}

int main(int argc, char* argv[]) {
    /* 参数检查 */
    if (argc != 4) {
        show_usage(argv[0]);
        return 1;
    }
    
    /* 判断操作类型 */
    if (strcmp(argv[1], "-c") == 0) {
        /* 压缩模式 */
        return compress_wav_file(argv[2], argv[3]);
    } else if (strcmp(argv[1], "-d") == 0) {
        /* 解压缩模式 */
        return decompress_wav_file(argv[2], argv[3]);
    } else {
        /* 无效的操作 */
        show_usage(argv[0]);
        return 1;
    }
} 